package com.teacold.customdemo.chapter_seven;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.teacold.customdemo.R;

import androidx.annotation.RequiresApi;

public class FilletHeadView  extends View {
    private Paint mPaint;
    private Bitmap mBitmap,bgBitmap;
    private String ImageUrl;
    private Context mContext;


    public FilletHeadView(Context context) {
        this(context,null);
    }

    public FilletHeadView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FilletHeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint=new Paint();
        mContext=context;
//        TypedArray array=context.obtainStyledAttributes(attrs,R.st)
        mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.dog);
    }

    public void setImageUrl(String imageUrl) {
        ImageUrl = imageUrl;
        Glide.with(mContext.getApplicationContext()).load(imageUrl).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                mBitmap=resource;
                bgBitmap=null;
                postInvalidate();
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
       if(bgBitmap==null){
           bgBitmap=Bitmap.createBitmap(getWidth(),getHeight(),Bitmap.Config.ARGB_8888);
           Canvas baCanvas=new Canvas(bgBitmap);
           baCanvas.drawBitmap(mBitmap,null,new Rect(0,0,getWidth(),getHeight()),mPaint);
       }
        mPaint.setShader(new BitmapShader(bgBitmap,Shader.TileMode.REPEAT,Shader.TileMode.REPEAT));
       canvas.drawRoundRect(0,0,getWidth(),getWidth(),20,20,mPaint);

    }
    //Gradient
}
